#include <bits/stdc++.h>
using namespace std;

bool check123(const string &s, int l, int r) {
	int c1 = 0, c2 = 0, c3 = 0;
	for (int i = l; i <= r; i++) {
		if (s[i] == '1') c1++;
		else if (s[i] == '2') c2++;
		else if (s[i] == '3') c3++;
	}
	return (c1 >= 1 && c2 >= 1 && c3 >= 1);
}

void solve() {
	int n;
	string s;
	cin >> n >> s;

	bool has1 = false, has2 = false, has3 = false;
	for (char c : s) {
		if (c == '1') has1 = true;
		else if (c == '2') has2 = true;
		else if (c == '3') has3 = true;
	}

	if (!has1 || !has2 || !has3) {
		cout << -1 << "\n";
		return;
	}

	bool ok = false;
	for (int i = 0; i + 2 < n; i++) {
		if (check123(s, i, i + 2)) {
			ok = true;
			break;
		}
	}

	if (ok) cout << 0 << "\n";
	else cout << 1 << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t; cin >> t;
	while (t--) solve();
	return 0;
}
